package com.qsbc.pygram.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsbc.pygram.common.Result;
import com.qsbc.pygram.entity.Student;
import com.qsbc.pygram.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-12-17
 */
@RestController
@RequestMapping("/pygram/student")
public class StudentController {
        @Autowired
        private StudentService studentService;

        @GetMapping
        public List<Student> findAll(){
            return studentService.list();
        }

        // 新增或者更新
        @PostMapping
        public Result save(@RequestBody Student student) {
            if (student.getId() == null && student.getPassword() == null) {  // 新增用户默认密码
                student.setPassword("123456");
            }
            return Result.success(studentService.saveOrUpdate(student));
        }

        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {

            return Result.success(studentService.removeById(id));
        }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {

        return Result.success(studentService.removeByIds(ids));
    }


        // 分页查询 - mybatis-plus的方式
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue ="") String username,@RequestParam(defaultValue ="") String sno) {

            return Result.success(studentService.findPage(new Page<>(pageNum, pageSize), username,sno));
        }
    }


